<template>
<div id="app">
    <router-view></router-view>
</div>
</template>

<script>
export default {
  name: 'App',

  provide() {
    return { //父组件通过provide来提供变量，在子组建通过inject来注入变量
      reload: this.reload
    }
  },

  data() {
    return {
      isRouterAlive: true //控制视图是否显示的变量
    }
  },

  created() {
    // 在页面加载时读取sessionStorage里的状态信息
    if (sessionStorage.getItem('store')) {
      this.$store.replaceState(
        Object.assign(
          {},
          this.$store.state,
          JSON.parse(sessionStorage.getItem('store'))
        )
      )
    }
    // 在页面刷新时将vuex里的信息保存到sessionStorage里避免，store丢失
    // beforeunload事件在页面刷新时先触发
    window.addEventListener('beforeunload', () => {
      sessionStorage.setItem('store', JSON.stringify(this.$store.state))
    })
  },

  methods: {
    reload() {
      this.isRouterAlive = false //先关闭
      this.$nextTick(function() {
        this.isRouterAlive = true //再打开
      })
    }
  }
}
</script>

<style lang="scss">
@import "./assets/css/index.scss";
body {
  margin: 0;
  padding: 0;
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
  overflow: scroll;
  overflow-y: hidden;
}

#app {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 100%;
}

.el-submenu [class^=fa] {
  vertical-align: baseline;
  margin-right: 10px;
}

.el-menu-item [class^=fa] {
  vertical-align: baseline;
  margin-right: 10px;
}

.toolbar {
  background: #f2f2f2;
  padding: 10px;
  margin: 10px 0;
  .el-form-item {
    margin-bottom: 10px;
  }
}
</style>
